package com.offics.loan.interfaces.controller;

import com.offics.loan.application.dto.repayment.RepayRequest;
import com.offics.loan.application.dto.repayment.RepayResponse;
import com.offics.loan.application.dto.repayment.RepaymentPlanResponse;
import com.offics.loan.application.dto.repayment.RepaymentRecordResponse;
import com.offics.loan.application.service.RepaymentApplicationService;
import com.offics.loan.infrastructure.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 还款控制器
 */
@Tag(name = "还款", description = "还款相关接口")
@RestController
@RequiredArgsConstructor
@SecurityRequirement(name = "JWT")
public class RepaymentController {

    private final RepaymentApplicationService repaymentApplicationService;

    /**
     * 还款
     *
     * @param request 还款请求
     * @return 还款响应
     */
    @Operation(summary = "还款", description = "还款")
    @PostMapping("/api/repayment-records/repay")
    public Result<RepayResponse> repay(@Valid @RequestBody RepayRequest request) {
        RepayResponse response = repaymentApplicationService.repay(request);
        return Result.success(response);
    }

    /**
     * 获取贷款的还款计划列表
     *
     * @param loanId 贷款ID
     * @return 还款计划列表
     */
    @Operation(summary = "获取贷款的还款计划列表", description = "获取贷款的还款计划列表")
    @GetMapping("/api/repayment-plans/loan/{loanId}")
    public Result<List<RepaymentPlanResponse>> getRepaymentPlanList(@PathVariable Long loanId) {
        List<RepaymentPlanResponse> responses = repaymentApplicationService.getRepaymentPlanList(loanId);
        return Result.success(responses);
    }

    /**
     * 获取用户的还款计划列表
     *
     * @param userId 用户ID
     * @return 还款计划列表
     */
    @Operation(summary = "获取用户的还款计划列表", description = "获取用户的还款计划列表")
    @GetMapping("/api/repayment-plans/user/{userId}")
    public Result<List<RepaymentPlanResponse>> getUserRepaymentPlanList(@PathVariable Long userId) {
        List<RepaymentPlanResponse> responses = repaymentApplicationService.getUserRepaymentPlanList(userId);
        return Result.success(responses);
    }

    /**
     * 获取还款计划详情
     *
     * @param id 还款计划ID
     * @return 还款计划详情
     */
    @Operation(summary = "获取还款计划详情", description = "获取还款计划详情")
    @GetMapping("/api/repayment-plans/{id}")
    public Result<RepaymentPlanResponse> getRepaymentPlanDetail(@PathVariable Long id) {
        RepaymentPlanResponse response = repaymentApplicationService.getRepaymentPlanDetail(id);
        return Result.success(response);
    }

    /**
     * 获取用户的还款记录列表
     *
     * @param userId 用户ID
     * @return 还款记录列表
     */
    @Operation(summary = "获取用户的还款记录列表", description = "获取用户的还款记录列表")
    @GetMapping("/api/repayment-records/user/{userId}")
    public Result<List<RepaymentRecordResponse>> getRepaymentRecordList(@PathVariable Long userId) {
        List<RepaymentRecordResponse> responses = repaymentApplicationService.getRepaymentRecordList(userId);
        return Result.success(responses);
    }

    /**
     * 获取贷款的还款记录列表
     *
     * @param loanId 贷款ID
     * @return 还款记录列表
     */
    @Operation(summary = "获取贷款的还款记录列表", description = "获取贷款的还款记录列表")
    @GetMapping("/api/repayment-records/loan/{loanId}")
    public Result<List<RepaymentRecordResponse>> getLoanRepaymentRecordList(@PathVariable Long loanId) {
        List<RepaymentRecordResponse> responses = repaymentApplicationService.getLoanRepaymentRecordList(loanId);
        return Result.success(responses);
    }

    /**
     * 获取还款记录详情
     *
     * @param id 还款记录ID
     * @return 还款记录详情
     */
    @Operation(summary = "获取还款记录详情", description = "获取还款记录详情")
    @GetMapping("/api/repayment-records/{id}")
    public Result<RepaymentRecordResponse> getRepaymentRecordDetail(@PathVariable Long id) {
        RepaymentRecordResponse response = repaymentApplicationService.getRepaymentRecordDetail(id);
        return Result.success(response);
    }
}
